//
//  huaiheMetadionDetailVC.m
//  huaihe
//
//  Created by 暗流澎涌 on 2024/3/8.
//

#import "huaiheMetadionDetailVC.h"
#import "huaiheEndView.h"
#import "huaiheCHooseTimeView.h"
#import "huaiheManger.h"
#import "AVFoundation/AVFoundation.h"
@interface huaiheMetadionDetailVC ()

@property (nonatomic,strong) NSArray * bgArr;
@property (nonatomic,strong) NSArray * titleArr;
@property (nonatomic,strong) NSArray * urlArr;

@property (weak, nonatomic) IBOutlet UIImageView *bgview;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UILabel *allTimeLabel;

@property (nonatomic,strong) huaiheEndView * tipsV;

@property (nonatomic,strong) huaiheCHooseTimeView * TimeView;

@property (weak, nonatomic) IBOutlet UIButton *starBut;

@property (weak, nonatomic) IBOutlet UIButton *endbut;

@property (weak, nonatomic) IBOutlet UIButton *startButNew;


@property (nonatomic,strong) AVPlayer * player;

@property (nonatomic,strong) AVPlayerItem * songItem;

@property (nonatomic,strong) NSTimer * timer;
@property (nonatomic,assign) NSInteger times;
@property (nonatomic,assign) NSInteger cunenttimes;
@end


@implementation huaiheMetadionDetailVC

-(huaiheEndView*)tipsV{
    if (!_tipsV){
        _tipsV = [[[NSBundle mainBundle] loadNibNamed:@"huaiheEndView" owner:self options:nil] lastObject];
        _tipsV.frame=CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    }
    return  _tipsV;
}

-(huaiheCHooseTimeView*)TimeView{
    if (!_TimeView){
        _TimeView = [[[NSBundle mainBundle] loadNibNamed:@"huaiheCHooseTimeView" owner:self options:nil] lastObject];
        _TimeView.frame=CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    }
    return  _TimeView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden: YES];
   
     
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden: NO];
    [self stopTimeer];
}
- (IBAction)gobackHme:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.bgArr = @[@"huaihe_bg1",@"huaihe_bg2",@"huaihe_bg3",@"huaihe_bg4",@"huaihe_bg5",@"huaihe_bg6"];
    self.titleArr = @[@"海浪",@"春雨",@"森林",@"鸟鸣",@"蛙鸣",@"轻音乐"];
    self.urlArr = @[@"http://huai.ktfgw.top/jingluo/海浪轻抚.mp3",@"http://huai.ktfgw.top/jingluo/小雨淅沥.mp3",@"http://huai.ktfgw.top/jingluo/鸟鸣.mp3",@"http://huai.ktfgw.top/jingluo/深林山风.mp3",@"http://huai.ktfgw.top/jingluo/蛙鸣.mp3",@"http://huai.ktfgw.top/jingluo/轻音乐.mp3"];

    self.bgview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.bgArr[self.tag-1]]];
    self.titleLabel.text = self.titleArr[self.tag -1];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(endmetaClick) name:@"endmeta" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(startContinu) name:@"startContinu" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];

  
    NSInteger timelong = [huaiheManger obtainmuicLong];
    self.times = timelong;
    self.cunenttimes = 0 ;
    if (timelong > 3599){
        if ((timelong%3600)/60 < 10){
            self.allTimeLabel.text = [NSString stringWithFormat:@"0%ld:0%ld:00",timelong/3600,(timelong%3600)/60];
        }else{
            self.allTimeLabel.text = [NSString stringWithFormat:@"0%ld:%ld:00",timelong/3600,(timelong%3600)/60];
        }
    }else{
        self.allTimeLabel.text = [NSString stringWithFormat:@"%ld:00",timelong/60];
    }
   
    [self.starBut setHidden: YES];
    [self.endbut setHidden: YES];
    NSString * urlString = [NSString stringWithFormat:@"%@",self.urlArr[self.tag-1]];
    NSString * encodeString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL * url = [NSURL URLWithString:encodeString];
    self.songItem = [[AVPlayerItem alloc]initWithURL:url];
    self.player = [[AVPlayer alloc]initWithURL:url];
    NSError *error = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
    [self starmed];

}
//重读播放、
- (void)moviePlayDidEnd:(NSNotification*)notification{
    
    AVPlayerItem*item = [notification object];

    [item seekToTime:kCMTimeZero];

    [self.player play];
}
//进来就开始冥想
-(void)starmed{
 
    [self playStart];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timego) userInfo:nil repeats:YES];
}



- (IBAction)backToUp:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

//选择时间
- (IBAction)SetAllTime:(id)sender {
    [self playpause];
    [self stopTimeer];
    [self.view addSubview:self.TimeView];
}


//结束
- (IBAction)endMeditaAction:(id)sender {
    [self.view addSubview:self.tipsV];
    [self.starBut setHidden: YES];
    
}
//暂停后继续
- (IBAction)startmeditionAction:(id)sender {
    [self.starBut setHidden: YES];
    [self.endbut setHidden: YES];
    [self.startButNew setHidden: NO];
    [self starmed];
}
//点击暂停
- (IBAction)startNew:(id)sender {
    //中间按钮
    [self.starBut setHidden: NO];
    [self.endbut setHidden: NO];
    [self.startButNew setHidden: YES];
    [self playpause];
    [self stopTimeer];
}
//继续播放
-(void)startContinu{
    NSInteger timelong = [huaiheManger obtainmuicLong];
    self.times = timelong;
    if (timelong > 3599){
        if ((timelong%3600)/60 < 10){
            self.allTimeLabel.text = [NSString stringWithFormat:@"0%ld:0%ld:00",timelong/3600,(timelong%3600)/60];
        }else{
            self.allTimeLabel.text = [NSString stringWithFormat:@"0%ld:%ld:00",timelong/3600,(timelong%3600)/60];
        }
        
    }else{
        self.allTimeLabel.text = [NSString stringWithFormat:@"%ld:00",timelong/60];
    }
//    self.allTimeLabel.text = [NSString stringWithFormat:@"%ld:00",timelong/60];
    [self.starBut setHidden: YES];
    [self.endbut setHidden: YES];
    [self.startButNew setHidden: NO];
    [self starmed];
}

//播放
-(void)playStart{
    [self.player play];
}
//暂停
-(void)playpause{
    [self.player pause];
}
-(void)stopTimeer{
    [self.timer invalidate];
     self.timer = nil;
}
-(void)timego{
    ++self.cunenttimes ;
    NSInteger hour =  self.cunenttimes/60;
    NSString * hourStr = @"00";
    NSInteger sec =  self.cunenttimes%60;
    NSString * secStr = @"00";
    if (hour < 10){
        hourStr = [NSString stringWithFormat:@"0%0ld",(long)hour];
    }else{
        hourStr = [NSString stringWithFormat:@"%0ld",(long)hour];
    }
    if (sec < 10 ){
        secStr = [NSString stringWithFormat:@"0%0ld",(long)sec];
    }else{
        secStr = [NSString stringWithFormat:@"%0ld",(long)sec];
    }
    self.timeLabel.text = [NSString stringWithFormat:@"%@:%@",hourStr,secStr];
   
}
-(void)endmetaClick{
    self.cunenttimes = 0;
    self.timeLabel.text = @"00:00";
    [self.starBut setHidden: YES];
    [self.endbut setHidden: YES];
    [self.startButNew setHidden: NO];
    [self stopTimeer];
    [self playpause];
    [self.navigationController popViewControllerAnimated:YES];
}



@end
